import Ellipsis from '@/components/Ellipsis'
import { formatMoneyNum, formatDate, formatDateTime } from '@/utils/displayHelper'

/**
 * @typedef {Object} ColumnsProps
 * @property {boolean} [exclude] 是否排除
 * @property {boolean} [isDate] 日期YYYY-MM-DD
 * @property {boolean} [isDateTime] 时间YYYY-MM-DD HH:mm:ss
 * @property {boolean} [isMoney] 钱
 * @property {import("@/utils/enum").default} [enum] 枚举
 */

/**
 * 帮助渲染表格项
 * @param {Array<import("antd/es/table").ColumnType & ColumnsProps>} columns
 */
export const helpColumnsRender = columns => {
  columns.forEach(column => {
    if (column.render) return
    const render = text => (typeof text === 'number' ? text || 0 : text || '-')
    column.render = render
    if (column.ellipsis) {
      column.render = value => <Ellipsis>{render(value)}</Ellipsis>
      delete column.ellipsis
    }
    if (column.isDate) {
      column.render = text => (text ? formatDate(text) : '-')
    }
    if (column.isDateTime) {
      column.render = text => (text ? formatDateTime(text) : '-')
    }
    if (column.isMoney) {
      column.render = value => <div className='c-money text-right'>{formatMoneyNum(value)}</div>
    }
    if (column.enum) {
      column.render = value => (
        <span style={{ color: column.enum.get(value, 'color') }}>{column.enum.getText(value)}</span>
      )
    }
  })
}
